package com.pcbly.web.components.utils.template;

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;

@SuppressWarnings("unchecked")

public class TemplateModel {
	
	private Class clz;

	private String entity;

	private String entityP;

	private String entityL;

	private String firstEntityL;

	private String clzName;

	public Class getClz() {
		return clz;
	}

	public void setClz(Class clz) {
		this.clz = clz;
	}

	public String getEntity() {
		entity=this.clzName.substring(this.clzName.lastIndexOf(".") + 1, this.clzName.length());
		return entity;
	}

	public void setEntity(String entity) {
		this.entity = entity;
	}

	public String getEntityP() {
		String t=getEntity();
		entityP = t.substring(0, t.length() - 4);
		return entityP;
	}

	public void setEntityP(String entityP) {
		this.entityP = entityP;
	}

	public String getEntityL() {
		String[] args = this.clzName.split("\\.");
		entityL = args[args.length - 2];
		return entityL;
	}

	public void setEntityL(String entityL) {
		this.entityL = entityL;
	}

	public String getFirstEntityL() {
		String t=getEntityP();
		firstEntityL = t.substring(0, 1).toLowerCase()
		+ t.substring(1, t.length());
		return firstEntityL;
	}

	public void setFirstEntityL(String firstEntityL) {
		this.firstEntityL = firstEntityL;
	}

	public String getClzName() {
		return clzName;
	}

	public void setClzName(String clzName) {
		this.clzName = clzName;
	}
	public String getFilePath(String packageName, String name) {
		// System.out.println("replace:" + packageName);
		String path = packageName.replaceAll("\\.", "/");
		// System.out.println("after relpace:" + path);
		return "src/" + path + "/" + name;
	}
	
	public String readTpl(File tpl) {
		return readTpl(tpl, "UTF-8");
	}

	public String readTpl(File tpl, String charset) {
		String content = null;
		try {
			content = FileUtils.readFileToString(tpl, charset);
			content = content.replaceAll("\\#\\{entityL\\}", getEntityL());
			content = content.replaceAll("\\#\\{entityP\\}", getEntityP());
			content = content.replaceAll("\\#\\{clzName\\}", clzName);
			content = content.replaceAll("\\#\\{entity\\}", getEntity());
			content = content.replaceAll("\\#\\{firstEntityL\\}",
					getFirstEntityL());
		} catch (IOException e) {
			System.out.println("read file faild. " + e.getMessage());
		}
		return content;
	}
}
